package com.sviet.game.tetriswindow.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.sviet.game.tetriswindow.R;
import com.sviet.game.tetriswindow.data.GlobalVariable;
import com.sviet.game.tetriswindow.util.Constants;
import com.sviet.game.tetriswindow.util.SharedPreferencesUtils;

public class NewHighScoreDialog extends Dialog implements OnClickListener {

	private String TAG = NewHighScoreDialog.class.getSimpleName();
	private Context context;
	private Button bt_replay;
	private Button bt_cancel;
	private TextView tv_title;
	private TextView tv_high_line;
	private TextView tv_high_score;
	private MyCountDown myCountDown;

	public NewHighScoreDialog(Context context) {
		super(context);
		// remove background dialog using style
		// super(context, R.style.Theme_Dialog_Translucent);
		// TODO Auto-generated constructor stub

		Log.v(TAG, "NewHighScoreDialog");
		this.context = context;
		Log.v(TAG, "this.context = " + this.context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.new_high_score_dialog);

		// remove background dialog
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));

		tv_title = (TextView) findViewById(R.id.new_high_score_tv_title);
		tv_high_line = (TextView) findViewById(R.id.new_high_score_tv_high_line);
		tv_high_score = (TextView) findViewById(R.id.new_high_score_tv_high_score);

		// Buttons
		bt_replay = (Button) findViewById(R.id.new_high_score_bt_replay);
		bt_replay.setOnClickListener(this);
		bt_cancel = (Button) findViewById(R.id.new_high_score_bt_cancel);
		bt_cancel.setOnClickListener(this);

		myCountDown = new MyCountDown(Constants.MILLIS_IN_FUTURE,
				Constants.COUNT_DOWN_INTERVAL);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();

		tv_high_line.setText(context.getString(R.string.your_line) + " "
				+ GlobalVariable.current_number_line);
		tv_high_score.setText(context.getString(R.string.your_score) + " "
				+ GlobalVariable.current_score);

		Log.v(TAG, "GlobalVariable.current_score = "
				+ GlobalVariable.current_score);
		Log.v(TAG,
				"high score = "
						+ SharedPreferencesUtils.GetPreference(context,
								SharedPreferencesUtils.SCORE));

		// Save score
		boolean isHighLine = false;
		if (GlobalVariable.current_number_line > SharedPreferencesUtils
				.GetPreference(context, SharedPreferencesUtils.LINE)) {
			isHighLine = true;
			SharedPreferencesUtils.SetPreference(context,
					SharedPreferencesUtils.LINE,
					GlobalVariable.current_number_line);
		}

		if (GlobalVariable.current_score > SharedPreferencesUtils
				.GetPreference(context, SharedPreferencesUtils.SCORE)) {
			SharedPreferencesUtils.SetPreference(context,
					SharedPreferencesUtils.SCORE, GlobalVariable.current_score);
			tv_title.setText(context.getString(R.string.new_high_score_message));
			myCountDown.start();
		} else {
			if (isHighLine) {
				tv_title.setText(context
						.getString(R.string.new_high_line_message));
				myCountDown.start();
			} else {
				tv_title.setText(context.getString(R.string.tr_again));
			}
		}
	}
	
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
		myCountDown.cancel();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.new_high_score_bt_replay) {
			GlobalVariable.isReplay = true;
			dismiss();
		} else if (v.getId() == R.id.new_high_score_bt_cancel) {
			dismiss();
		}
	}

	private class MyCountDown extends CountDownTimer {

		public MyCountDown(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onFinish() {
			// TODO Auto-generated method stub
			this.start();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			// TODO Auto-generated method stub
			if (tv_title.getCurrentTextColor() == Color.YELLOW) {
				tv_title.setTextColor(Color.RED);
			} else if (tv_title.getCurrentTextColor() == Color.RED) {
				tv_title.setTextColor(Color.YELLOW);
			}
		}

	}
}
